#include "SceneView.h"
#include "Scene.h"
#include <QtGui>

SceneView::SceneView(QWidget* parent, Scene* scene)
	: QAbstractScrollArea(parent), m_pScene(scene), m_CurrentTool(T_MOVE)
{
}

SceneView::~SceneView(void)
{
}

void SceneView::setCurrentTool(TOOL tool) {
	m_CurrentTool = tool;
	switch(m_CurrentTool) {
	case T_MOVE:
		setCursor(Qt::ArrowCursor);
		break;
	case T_MOVEVIEW:
		setCursor(Qt::OpenHandCursor);
		break;
	case T_SCALEVIEW:
		setCursor(Qt::CrossCursor);
		break;
	}
}

void SceneView::paintEvent(QPaintEvent* event) {
	QWidget::paintEvent(event);
	QPainter painter(viewport());
	m_pScene->paint(painter);
}

void SceneView::mousePressEvent(QMouseEvent* event) {
	switch(m_CurrentTool) {
	case T_MOVE:
		m_pScene->beginMove(event);
		break;
	case T_MOVEVIEW:
		m_pScene->beginMoveView(event);
		setCursor(Qt::ClosedHandCursor);
		break;
	case T_SCALEVIEW:
		m_pScene->beginScaleView(event);
		break;
	}
}

void SceneView::mouseMoveEvent(QMouseEvent* event) {
	switch(m_CurrentTool) {
	case T_MOVE:
		m_pScene->doMove(event);
		break;
	case T_MOVEVIEW:
		m_pScene->doMoveView(event);
		break;
	case T_SCALEVIEW:
		m_pScene->doScaleView(event);
		break;
	}
}

void SceneView::mouseReleaseEvent(QMouseEvent* event) {
	switch(m_CurrentTool) {
	case T_MOVE:
		m_pScene->endMove(event);
		break;
	case T_MOVEVIEW:
		m_pScene->endMoveView(event);
		setCursor(Qt::OpenHandCursor);
		break;
	case T_SCALEVIEW:
		m_pScene->endScaleView(event);
		break;
	}
}

void SceneView::keyPressEvent(QKeyEvent* event) {
	m_pScene->keyPressEvent(event);
}

void SceneView::keyReleaseEvent(QKeyEvent *event) {
	m_pScene->keyReleaseEvent(event);
}
